package com.android.topview.watermelon.exchange

import android.content.Context
import android.os.Bundle
import android.view.inputmethod.InputMethodManager
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.ViewModelProvider
import com.android.topview.watermelon.R
import com.android.topview.watermelon.base.BaseActivity
import com.android.topview.watermelon.util.setTalkBackFocus
import com.android.topview.watermelon.util.showToast
import com.android.topview.watermelon.util.toChinese
import kotlinx.android.synthetic.main.activity_exchange_result.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class ExchangeResultActivity : BaseActivity() {

    private lateinit var back: ImageView
    private lateinit var from: TextView
    private lateinit var to: TextView
    private lateinit var scan: EditText
    private lateinit var exchange: Button
    private lateinit var result: TextView

    private lateinit var vm: ExchangeResultViewModel

    private lateinit var fromCode: ExchangeCode
    private lateinit var toCode: ExchangeCode

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_exchange_result)
        init()
    }

    @Suppress("SetTextI18n")
    private fun init() {
        vm = ViewModelProvider(this).get(ExchangeResultViewModel::class.java)

        back = iv_exchange_result_back
        scan = et_exchange_result_from_amount
        from = tv_exchange_result_from
        to = tv_exchange_result_to
        exchange = btn_exchange_result_exchange
        result = tv_exchange_result_result

        fromCode = EXCHANGE_CODE_ARRAY[intent.getIntExtra("from", 1)]
        toCode = EXCHANGE_CODE_ARRAY[intent.getIntExtra("to", 0)]

        back.setOnClickListener {
            finish()
        }

        from.text = "${fromCode.msg} ${fromCode.code}"
        to.text = "${toCode.msg} ${toCode.code}"

        from.contentDescription =
            "${getString(R.string.exchange_from)}为，${fromCode.msg}"

        to.contentDescription =
            "${getString(R.string.exchange_to)}为，${toCode.msg}"

        launch(Dispatchers.IO) {
            vm.getExchangeRate(fromCode, toCode)
        }

        exchange.setOnClickListener {
            (getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)
                .hideSoftInputFromWindow(window.decorView.windowToken, 0)
            vm.exchangeRate.value?.let {
                if (it.first == fromCode && it.second == toCode) {
                    when (it.third) {
                        -1.0 -> showToast("正在获取汇率，请稍后")
                        -2.0 -> showToast("获取汇率时网络错误")
                        -3.0 -> showToast("获取汇率时发生未知错误")
                        else ->
                            try {
                                val scanNum = scan.text.toString().toLong()
                                val fromStr = scanNum.toString().toChinese()
                                (scanNum / it.third).let { r ->
                                    if (r >= 0.01) {
                                        val res = String.format("%.2f", r)
                                        val toStr = res.toChinese()
                                        val resultStr =
                                            "$fromStr${fromCode.msg}相当于${toStr}${toCode.msg}"
                                        showToast(resultStr)
                                        result.text =
                                            "${scanNum}${fromCode.msg}\n相当于\n${res}${toCode.msg}"
                                        result.contentDescription =
                                            "$fromStr${fromCode.msg}相当于${toStr}${toCode.msg}"
                                        result.contentDescription = resultStr
                                    } else showToast("兑换后小于 0.01 元")
                                }
                            } catch (e: NumberFormatException) {
                                showToast("输入持有币种数量过大或有误，请重新输入")
                            }
                    }
                }
            }
        }

        tv_exchange_result_title.setTalkBackFocus()

    }

}